package com.company.cloud.user.modules.mgr.service.impl;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.user.modules.base.entity.TeamInfo;
import com.company.cloud.user.modules.base.entity.TeamUserInfo;
import com.company.cloud.user.modules.base.service.TeamInfoService;
import com.company.cloud.user.modules.base.service.TeamUserInfoService;
import com.company.cloud.user.modules.mgr.controller.req.TeamMgrSaveParam;
import com.company.cloud.user.modules.mgr.controller.req.TeamMgrUpdateParam;
import com.company.cloud.user.modules.mgr.controller.req.TeamUserMgrAddParam;
import com.company.cloud.user.modules.mgr.controller.req.TeamUserMgrDelParam;
import com.company.cloud.user.modules.mgr.service.TeamMgrService;

@Service
public class TeamMgrServiceImpl implements TeamMgrService{

	@Autowired
	private TeamInfoService teamInfoService;
	@Autowired
	private TeamUserInfoService teamUserInfoService;
	
	@Transactional
	@Override
	public void save(TeamMgrSaveParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		TeamInfo entity=new TeamInfo();
		entity.setCompanyNo(params.getCompanyNo());
		entity.setLeaderUserId(params.getLeaderUserId());
		entity.setType(params.getType());
		entity.setName(params.getName());
		entity.setRemark(params.getRemark());
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		teamInfoService.save(entity);
		
		if(!CollectionUtils.isEmpty(params.getUserIds())) {
			Set<TeamUserInfo> teamUserSet=new HashSet<TeamUserInfo>();
			for(Integer userId:params.getUserIds()) {
				if(userId!=null) {
					TeamUserInfo teamUser=new TeamUserInfo();
					teamUser.setCompanyNo(entity.getCompanyNo());
					teamUser.setTeamId(entity.getId());
					teamUser.setUserId(userId);
					teamUser.addEditParam(userInfo);
					teamUser.addCreateParam(userInfo);
					teamUserSet.add(teamUser);
				}
			}
			if(teamUserSet.size()>0) {
				teamUserInfoService.saveBatch(teamUserSet);
			}
		}
		
	}
	@Override
	public void update(TeamMgrUpdateParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		TeamInfo entity= teamInfoService.getById(params.getId());
		if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity.setLeaderUserId(params.getLeaderUserId());
		entity.setType(params.getType());
		entity.setName(params.getName());
		entity.setRemark(params.getRemark());
		entity.addEditParam(userInfo);
		teamInfoService.updateById(entity);
		
		if(!CollectionUtils.isEmpty(params.getUserIds())) {
			Set<TeamUserInfo> teamUserSet=new HashSet<TeamUserInfo>();
			for(Integer userId:params.getUserIds()) {
				if(userId!=null) {
					TeamUserInfo teamUser=new TeamUserInfo();
					teamUser.setCompanyNo(entity.getCompanyNo());
					teamUser.setTeamId(entity.getId());
					teamUser.setUserId(userId);
					teamUser.addEditParam(userInfo);
					teamUser.addCreateParam(userInfo);
					teamUserSet.add(teamUser);
				}
			}
			if(teamUserSet.size()>0) {
				teamUserInfoService.remove(new QueryWrapper<TeamUserInfo>()
						.eq("company_no", entity.getCompanyNo())
						.eq("team_id", entity.getId()));
				teamUserInfoService.saveBatch(teamUserSet);
			}
		}
	}
	@Override
	public void delete(Integer id, LoginSimpleUserInfo userInfo) {
		TeamInfo entity= teamInfoService.getById(id);
		if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity.setRowsState(EnabledEnum.DISABLED.getCode());
		teamInfoService.updateById(entity);
		teamUserInfoService.remove(new QueryWrapper<TeamUserInfo>()
				.eq("company_no", entity.getCompanyNo())
				.eq("team_id", entity.getId()));
	}
	
	@Override
	public void addUsers(TeamUserMgrAddParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		TeamInfo teamInfo= teamInfoService.getById(params.getTeamId());
		if (teamInfo == null || EnabledEnum.DISABLED.getCode().equals(teamInfo.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		
		if(!CollectionUtils.isEmpty(params.getUserIds())) {
			Set<TeamUserInfo> teamUserSet=new HashSet<TeamUserInfo>();
			for(Integer userId:params.getUserIds()) {
				if(userId!=null) {
					TeamUserInfo teanUser=new TeamUserInfo();
					teanUser.setCompanyNo(teamInfo.getCompanyNo());
					teanUser.setTeamId(teamInfo.getId());
					teanUser.setUserId(userId);
					teanUser.addEditParam(userInfo);
					teanUser.addCreateParam(userInfo);
					teamUserSet.add(teanUser);
				}
			}
			if(teamUserSet.size()>0) {
				teamUserInfoService.saveBatch(teamUserSet);
			}
		}
	}
	
	@Override
	public void delUsers(TeamUserMgrDelParam params, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(params);
		teamUserInfoService.removeByIds(params.getTeamUserIds());
	}
	
	
	
}
